package com.ast;

import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeTranslator;
import com.sun.tools.javac.util.List;

/**
 * 自定义TreeTranslator
 * @author yutao
 * @since 2020/4/21 6:17 下午
 */
public class LogClearTranslator extends TreeTranslator {

    public static final String LOG_TAG = "Log.";

    /**
     * 访问块结构
     * @param jcBlock
     */
    @Override
    public void visitBlock(JCTree.JCBlock jcBlock) {
        super.visitBlock(jcBlock);
        List<JCTree.JCStatement> statements = jcBlock.getStatements();
        if (statements == null || statements.isEmpty()) {
            return;
        }
        iterateList(jcBlock, statements);
    }

    private void iterateList(JCTree.JCBlock jcBlock, List<JCTree.JCStatement> statements) {
        List<JCTree.JCStatement> out = List.nil();
        for (JCTree.JCStatement statement : statements) {

            if (!isLogStatements(statement)) {
                out = out.append(statement);
            }
        }
        jcBlock.stats = out;
    }

    private boolean isLogStatements(JCTree.JCStatement statement) {
        return statement.toString().contains(LOG_TAG);
    }
}
